//////////////////////////////////////////////////////////////////////////////////////////////
//Class Name: listOfEmployeesController
//Autor: Víctor Galván Chávez
//Objective: This class shows an employees list and lets delete them or view their information
//////////////////////////////////////////////////////////////////////////////////////////////

public class listOfEmployeeController {
        
        //Attributes
    public String idToUpdate{get;set;}
    public String idToView{get;set;}
    //public Integer numberOfEmployees{get; set;}
    public Integer numberToCompleteList{get; set;}
    public List<Account> employees2{get;set;}
    //public Integer n{get;set;}
    public Integer numberOfEmployees{
        get{
                        numberOfEmployees = employees2.size();
                        return numberOfEmployees;
        }
        set;
    }
    
    private String soql {get;set;}
    
    //*Objective: This is the constructor of the class. First it initializes the soql variable and then
    //run the query showing all the employees.
    public listOfEmployeeController(){
        soql = 'SELECT Id,AccountNumber, Salutation, LastName, FirstName, Join_date__c, IsPersonAccount, Position__c, Position__r.Name, Current_store__c, Current_store__r.Name, Face_Photo_ID__c FROM Account WHERE Active__c=true AND Employee_Status__c=\'Active\'';
        runQuery();
    }
    
        //*Objective: This method updates the "soql" variable appending search criteria
    //and then call "runQuery" method.
    public PageReference runSearch() {
        String firstName= Apexpages.currentPage().getParameters().get('firstName');
        //System.debug('######################################FIRST NAME PARAMETER: '+firstName);
        String lastName= Apexpages.currentPage().getParameters().get('lastName');
        String selZona= Apexpages.currentPage().getParameters().get('selZona');
        String selStore= Apexpages.currentPage().getParameters().get('selStore');
        String selPos= Apexpages.currentPage().getParameters().get('selPos');
        soql = 'SELECT Id,AccountNumber, Salutation, LastName, FirstName, Join_date__c, IsPersonAccount, Position__c, Position__r.Name, Current_store__c, Current_store__r.Name, Current_Store__r.Area__r.Name, Face_Photo_ID__c FROM Account WHERE Active__c=true AND Employee_Status__c=\'Active\'';
        if (!firstName.equals(''))
        soql += ' and FirstName LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
        soql += ' and LastName LIKE \''+String.escapeSingleQuotes(lastName)+'%\''; 
        if (!selZona.equals(''))
        soql += ' and Current_Store__r.Area__r.Name LIKE \''+selZona+'\'';
        if (!selStore.equals(''))
        soql += ' and Current_Store__r.Name LIKE \''+selStore+'\'';
        if (!selPos.equals(''))
        soql += ' and Position__r.Name LIKE \''+selPos+'\'';
    
        // run the query again
        runQuery();
        return null;
    }
    
    //*Objective: This method searchs employees depending of the "soql" variable
    public void runQuery() {
        try {
            employees2 = Database.query(soql + ' ORDER BY Join_date__c DESC');
            numberOfEmployees= employees2.size();
            //n=employees2.size();
            numberToCompleteList = 4 - (Math.mod(numberOfEmployees,4));
            if(Math.mod(numberOfEmployees,4)==0)numberToCompleteList=0;
           
        } catch (Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        
        } 
    }
    
/////////////////////////////////////////////Get and Set Methods////////////////////////////////////
    public List<Area__c> selectZona{
        get{
            if(selectZona == null){
                selectZona = [SELECT Name FROM Area__c];
            }
            return selectZona;
        }
        set;
    }
    
    public List<Store__c> selectStore{
        get{
            if(selectStore == null){
                selectStore = [SELECT Name FROM Store__c];
            }
            return selectStore;
        }
        set;
    }
    
    public List<Position__c> selectPos{
        get{
            if(selectPos == null){
                selectPos = [SELECT Name FROM Position__c];
            }
            return selectPos;
        }
        set;
    }
        
    public List<Account> employees{   
        get{
            if(employees==null){
            employees = [SELECT Id,AccountNumber, Salutation, LastName, FirstName, Join_date__c, 
                        IsPersonAccount, Position__c, Position__r.Name, Current_store__c, Current_store__r.Name,
                        Face_Photo_ID__c FROM Account WHERE Active__c=true AND Employee_Status__c='Active' ORDER BY Join_date__c DESC];
                        
            }
            numberOfEmployees = employees.size();
            numberToCompleteList = 4 - (Math.mod(numberOfEmployees,4));
            if(Math.mod(numberOfEmployees,4)==0)numberToCompleteList=0;
            return employees;
        }
        set;
    }
/////////////////////////////////////////////////////////////////////////////////////////////////
    
    //*Objective: This method sends the user to wizard to edit information of an employee
    public PageReference editEmployee(){
        try{
            PageReference editPage = Page.EmployeeWizardStep1;
            editPage.getParameters().put('id',idToUpdate);
            editPage.setRedirect(true);
            return editPage;
        }catch(Exception ex){
            System.debug('An error has ocurred : listOfEmployeeController: editEmployee: '+ex.getMessage());
            return null;
        }
    }
    //*Objective: This method sends the user to wizard to view information of an employee
     public PageReference viewEmployee(){
        try{
            PageReference viewPage = Page.accountQueryView;
            viewPage.getParameters().put('id',idToView);
            viewPage.setRedirect(true);
            return viewPage;
        }catch(Exception ex){
            System.debug('An error has ocurred : listOfEmployeeController: viewEmployee: '+ex.getMessage());
            return null;
        }
    }
    
    //*Objective: This attribute creates the buttons depending of the number of employees 
    public List<ButtonCreator> buttons {
        get{
                        
                        buttons = new List<ButtonCreator>();
                        Integer numberOfButtons = (numberOfEmployees/12);
                        if(Math.mod(numberOfEmployees,12)==0){
                                
                                numberOfButtons-=1;
                                
                        }
                        Integer sizeScroll = 0;
                        
                        if(numberOfEmployees>12){
                                for(Integer i=0;i<=numberOfButtons;i++){
                                    ButtonCreator button = new ButtonCreator();
                                    button.numberOfButton = String.ValueOf(i+1);
                                    button.scrolling = String.valueOf(sizeScroll);
                                    buttons.add(button);
                                    sizeScroll+=390;
                                }
                        }
                        
                
            return buttons;
        }
        set;
    }
    
    
}